package com.xxs.controller.admin;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

import com.jfinal.aop.Before;
import com.xxs.common.Const;
import com.xxs.interceptor.AdminInterceptor;
import com.xxs.utils.UUIDUtils;
import com.xxs.utils.xml.Fields;
import com.xxs.utils.xml.XmlDom4jUtils;

@Before(AdminInterceptor.class)
@SuppressWarnings("unchecked")
public class AttributeController extends BaseAdminSiteController {

	public void list(){
		String modelName = getModeName();
		Element element = XmlDom4jUtils.getRootElement(getXmlPath(getCurrentSiteCode(), modelName));
		List<Element> list = element.elements();
		setAttr("fieldList", XmlDom4jUtils.getFields(Const.SYSTEM_TABLES_PATH,modelName));
    	setAttr("list", list);
    	render("list.html");
    }
	
	public void save(){
		String modelName = getModeName();
		List<Fields> fieldList = getFields();
		Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(), modelName));
		Element element = document.getRootElement();
		Element newElement = element.addElement(modelName);
		newElement.setName(modelName);
		for (int i = 0; i < fieldList.size(); i++) {
			System.out.println("当前循环的属性名："+fieldList.get(i).getName()+"填充的内容"+getPara(fieldList.get(i).getName()));
			if("id".equals(fieldList.get(i).getName())){
				newElement.addAttribute(fieldList.get(i).getName(), UUIDUtils.creatUUID());
			}else{
				newElement.addAttribute(fieldList.get(i).getName(), getPara(fieldList.get(i).getName()));
			}
		}
		try {
			XmlDom4jUtils.saveDocumentToFile(getXmlPath(getCurrentSiteCode(), modelName), document);
		} catch (Exception e) {
			e.printStackTrace();
		}
		list();
	}
}
